package cloud.dandanit.itservice.controller.image;


import cloud.dandanit.itservice.entity.Article;
import cloud.dandanit.itservice.entity.Banner;
import cloud.dandanit.itservice.entity.Image;
import cloud.dandanit.itservice.service.ImageService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author luokai
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/itservice/imageadmin")
public class ImageAdminController {

    @Autowired
    private ImageService imageService;


    /**
     * 上传图片到腾讯云COS中，仅限于在上传el-upload的action中
     * @param file 文件
     * @return 文件链接
     */
    @PostMapping
    @ApiOperation("图片上传接口")
    public R uploadImage(
            @ApiParam(name = "file", value = "前端文件", required = true)
            MultipartFile file) {

        //获取上传文件 MultipartFile
        //返回上传的图片cos路径
        String url = imageService.uploadImage(file);

        return R.ok().data("url", url);
    }

    /**
     * 获取图片方法
     * @return 图片列表
     */
    @GetMapping("getAllImage")
    @ApiOperation("获取图片接口")
    public R getAllImage() {

        //获取上传文件 MultipartFile
        //返回上传的图片oss路径
        List<Image> imageList = imageService.getAllImage();
        return R.ok().data("imageList", imageList);
    }


    @ApiOperation(value = "获取图片分页列表")
    @GetMapping("pageImage/{page}/{limit}")
    public R getPageImage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Image> pageParam = new Page<>(page, limit);
        imageService.page(pageParam,null);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @ApiOperation(value = "新增图片")
    @PostMapping("addImage")
    public R addImage(
            @ApiParam(name = "image", value = "图片实体", required = true)
            @RequestBody Image image) {

        //生成随机大小(KB)kb
        long randomSize= (long) (25+Math.random()*(50-25+1));
        image.setSize(randomSize);
        imageService.save(image);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取图片")
    @GetMapping("getImageById/{id}")
    public R getById(
            @ApiParam(name = "id", value = "图片ID", required = true)
            @PathVariable String id) {

        Image image = imageService.getById(id);
        return R.ok().data("image", image);
    }

    @ApiOperation(value = "修改图片")
    @PostMapping("updateImage")
    public R update(
            @ApiParam(name = "image", value = "图片实体", required = true)
            @RequestBody Image image) {

        imageService.updateById(image);
        return R.ok();
    }

    @ApiOperation(value = "删除图片")
    @DeleteMapping("removeImageById/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "图片ID", required = true)
            @PathVariable String id) {

        imageService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "更改文章状态")
    @GetMapping("changeState/{imageId}")
    public R changeState(
            @ApiParam(name = "imageId", value = "图片ID", required = true)
            @PathVariable String imageId) {

        Image image = imageService.getById(imageId);
        if (image.getState()==0) {
            image.setState(1);
        } else {
            image.setState(0);
        }
        imageService.updateById(image);

        return R.ok();
    }

    @ApiOperation(value = "一键修改图片状态")
    @GetMapping("batchChangeImageState/{state}")
    public R batchChangeImageState(
            @ApiParam(name = "state", value = "更改的状态", required = true)
            @PathVariable Integer state) {

        List<Image> imageList = imageService.list(null);
        for (Image image : imageList) {
            image.setState(state);
        }
        imageService.updateBatchById(imageList);
        return R.ok();
    }


}

